﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PRO.DbProvider;
using PRO.Entity;
using Newtonsoft.Json;
using PRO.Common;
using PRO.Config;
using System.Data;
using PRO.ORM;

namespace PRO.User
{
    public class UserLib
    {
        /// <summary>
        /// 检查邮箱是否被注册了
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static bool IsExistUser(string username)
        {
            return DataBaseProvider.GetInstance().IsExistUser(username);
        }
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="ui"></param>
        /// <returns></returns>
        public static bool AddUser(UserInfo ui)
        {
            int id = DataBaseProvider.GetInstance().AddUser(ui);
            if (id > 0)
            {
                ui.ID = id;
                UserLib.WriteCookie(ui);
                return true;
            }
            else
                return false;
        }
        public static bool Login(string username, string pwd,out string msg)
        {
            bool r = false;
            msg = "";
            UserInfo ui = UserLib.GetUserInfo(username);
            if (ui != null && ui.ID > 0)
            {
                if (ui.Pwd.Equals(pwd))
                {
                    UserLib.WriteCookie(ui);
                    r = true;
                }
                else
                    msg = "密码不正确！";
            }
            else
                msg = "该用户不存在！";
            return r;
        }
        /// <summary>
        /// 登陆成功后写Cookie
        /// </summary>
        /// <param name="ui"></param>
        private static void WriteCookie(UserInfo ui)
        {
            string guid = Guid.NewGuid().ToString();
            CookieInfo info = new CookieInfo();
            info.ID = ui.ID;
            info.Nick = ui.Nick;
            info.UserName = ui.UserName;
            info.Pwd = ui.Pwd;
            info.Guid = guid.DEC_Encode(BaseConfigs.DEC_KEY);
            Utils.WriteCookie("UserInfo".DEC_Encode(BaseConfigs.DEC_KEY), JsonConvert.SerializeObject(info).DEC_Encode(BaseConfigs.DEC_KEY));

            Utils.WriteCookie("CheckCode".DEC_Encode(BaseConfigs.DEC_KEY), Utils.MD5(guid), 60 * 24);
        }

        /// <summary>
        /// 获取登陆用户信息
        /// </summary>
        /// <returns></returns>
        public static CookieInfo GetUserInfo()
        {
            CookieInfo ui = new CookieInfo();
            string json = Utils.GetCookie("UserInfo".DEC_Encode(BaseConfigs.DEC_KEY));
            string code = Utils.GetCookie("CheckCode".DEC_Encode(BaseConfigs.DEC_KEY));
            if (!code.IsNullOrWhiteSpace() && !json.IsNullOrWhiteSpace())
            {
                try
                {
                    ui = JsonConvert.DeserializeObject<CookieInfo>(json.DEC_Decode(BaseConfigs.DEC_KEY));
                    if (ui != null)
                        if (!Utils.MD5(ui.Guid.DEC_Decode(BaseConfigs.DEC_KEY)).Equals(code))
                        {
                            ui.ID = 0;
                        }
                }
                catch
                {
                    ui.ID = 0;
                }
            }
            return ui;
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static UserInfo GetUserInfo(string username)
        {
            UserInfo ui = new UserInfo();
           DataTable dt=  DataBaseProvider.GetInstance().GetUserInfo(username);
           if (dt != null)
               ui.BindDataToEntity(dt);
           return ui;
        }
    }
}
